package com.yudean.esb.service.impl.erp;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.yudean.esb.dao.erp.ErpIdsInsDangerTypeDao;
import com.yudean.esb.domain.erp.ErpIdsInsDangerType;
import com.yudean.esb.service.ErpIdsInsDangerTypeService;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jws.WebService;
import java.util.Iterator;

import static com.yudean.esb.util.XmlUtil.returnInfo;
/**
 * Created by zenghui on 2015-05-19.
 * desc:
 */
@WebService(endpointInterface = "com.yudean.esb.service.ErpIdsInsDangerTypeService", serviceName = "ErpIdsInsDangerTypeService",targetNamespace = "http://service.esb.yudean.com/")
@Service("erpIdsInsDangerTypeServiceImpl")
@Scope("prototype")
public class ErpIdsInsDangerTypeServiceImpl implements ErpIdsInsDangerTypeService {
    private static final String TAG = "ErpIdsInsDangerTypeServiceImpl";
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ErpIdsInsDangerTypeServiceImpl.class);
    private static final String SERVICE_NAME = "险种信息";
    private static final String SERVICE_TYPE = "写入数据";
    @Resource
    private ErpIdsInsDangerTypeDao erpIdsInsDangerTypeDao;


    /**
     *
     * @param dangerTypeXML
     * 格式：
     *  <?xml version="1.0" encoding="UTF-8"?>
     *   <service>
     *       <name>险种信息</name>
     *       <type>写入数据</type>
     *       <data>
     *           <ErpIdsInsDangerType>
     *               <insTypeCode>InsTypeCode</insTypeCode>
     *               <insTypeName>insName</insTypeName>
     *               <creationDate>2015-05-19 07:36:28.620 UTC</creationDate>
     *               <createdBy>11</createdBy>
     *               <lastUpdateDate>2015-05-19 07:36:28.620 UTC</lastUpdateDate>
     *               <lastUpdatedBy>11</lastUpdatedBy>
     *               <attribute1>attribute1</attribute1>
     *               <attribute2>attribute2</attribute2>
     *               <attribute3>attribute3</attribute3>
     *               <attribute4>attribute4</attribute4>
     *               <attribute5>attribute5</attribute5>
     *           </ErpIdsInsDangerType>
     *           <ErpIdsInsDangerType>
     *               ...
     *               <createdBy>11</createdBy>
     *               <lastUpdateDate>2015-05-19 07:36:28.620 UTC</lastUpdateDate>
     *               <lastUpdatedBy>11</lastUpdatedBy>
     *               <attribute1>attribute1</attribute1>
     *               ...
     *           </ErpIdsInsDangerType>
     *           ...
     *       </data>
     *   </service>
     *
     * @return
     */


    @Override
    public String putDangerType(String dangerTypeXML) {
        if (StringUtils.isBlank(dangerTypeXML)) {
            return returnInfo(SERVICE_NAME,SERVICE_TYPE,"blank params");
        }
        try {
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("ErpIdsInsDangerType", ErpIdsInsDangerType.class);
            Document dom = DocumentHelper.parseText(dangerTypeXML);
            Element root = dom.getRootElement();
            Element dataList = root.element("data");
            for (Iterator i = dataList.elementIterator(); i.hasNext();) {
                Element data = (Element) i.next();
                String eleXml = data.asXML();
                ErpIdsInsDangerType erpIdsInsDangerType = (ErpIdsInsDangerType) xstream.fromXML(eleXml);
                erpIdsInsDangerTypeDao.insert(erpIdsInsDangerType);
            }
            return returnInfo(SERVICE_NAME, SERVICE_TYPE,"insert success");
        } catch (Exception e) {
            return returnInfo(SERVICE_NAME, SERVICE_TYPE,"insert error" + e.getMessage());
        }
    }
}
